package cn.tedu.boot07.controller;

import cn.tedu.boot07.entity.Weibo;
import cn.tedu.boot07.mapper.WeiboMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.util.Date;
import java.util.List;

@RestController
public class WeiboController {


    @Autowired
    private WeiboMapper mapper;

    @RequestMapping("/insert")
    public void insert(@RequestBody Weibo weibo){
        System.out.println("weibo:"+weibo);

        // new Date()  获取当前时间
        weibo.setCreated(new Date());

        mapper.insert(weibo);


    }

    @RequestMapping("/select")
    public List<Weibo> select(){

        List<Weibo> list = mapper.select();

        return list;
    }

    @RequestMapping("/delete")
    public void delete(int id){

        String url = mapper.selectUrlById(id);
        new File("e:/temp"+url).delete();
        mapper.deleteById(id);
    }

}
